using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ShrapnelTest
{
    public partial class MainForm : Form
    {
        private string shrapnelPath = string.Empty;

        public MainForm()
        {
            InitializeComponent();

			// Register all our own node element types
			workspaceView.RegisteredTypes.Add(typeof(TestNode));
			workspaceView.RegisteredTypes.Add(typeof(TestCable));
			workspaceView.RegisteredTypes.Add(typeof(TestConnector));


            // Add some random nodes and cables for testing purposes

            Shrapnel.Layer newLayer = new Shrapnel.Layer();

            Random r = new Random(0); // use a fixed seed, so we always have the same results

            List<PointF> spots = new List<PointF>();
            List<Shrapnel.Connector> availableConnectors = new List<Shrapnel.Connector>();

            for (int y = 0; y < 10; y++)
            {
                for (int x = 0; x < 10; x++)
                {
                    PointF spot = new PointF(x * 150.0f, y * 120.0f);
                    spots.Add(spot);
                }
            }

            for (int i = 0; i < 20; ++i)
            {
                int spotIndex = (int)(r.NextDouble() * spots.Count);


                TestNode newNode = new TestNode(spots[spotIndex]);
                newLayer.Add(newNode);

                foreach (Shrapnel.Connector connector in newNode.Connectors)
                {
                    availableConnectors.Add(connector);
                }

                spots.RemoveAt(spotIndex);
            }

            for (int i = 0; i < 20; ++i)
            {
                int fromIndex = (int)(r.NextDouble() * availableConnectors.Count);
                Shrapnel.Connector fromConnector = availableConnectors[fromIndex];
                availableConnectors.RemoveAt(fromIndex);

                int toIndex = (int)(r.NextDouble() * availableConnectors.Count);
                Shrapnel.Connector toConnector = availableConnectors[toIndex];
                availableConnectors.RemoveAt(toIndex);

                TestCable newCable = new TestCable(fromConnector, toConnector);
                newLayer.Add(newCable);
            }

            workspaceView.AddLayer(newLayer);

            workspaceView.ZoomAllInView();
        }


		private Shrapnel.Cable workspaceView_NewCableRequired(Shrapnel.Connector from, Shrapnel.Connector to)
		{
			return new TestCable(from, to);
        }

        private void OnNewWorkspaceView(object sender, EventArgs e)
        {
            if (workspaceView.IsDirty)
            {
                DialogResult result = MessageBox.Show(null, "View has been changed, do you want to save?", Program.Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                switch (result)
                {
                    case DialogResult.Yes:
                        OnSaveAsWorkspaceView(sender, e);
                        break;

                    case DialogResult.No:
                        break;

                    case DialogResult.Cancel:
                        return;
                }
            }

            shrapnelPath = string.Empty;
			workspaceView.Reset();
        }

        private void OnOpenWorkspaceView(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Filter = "Shrapnel Files (*.shrapnel)|*.shrapnel|All Files (*.*)|*.*";
            dialog.Multiselect = false;
            dialog.RestoreDirectory = true;
            dialog.SupportMultiDottedExtensions = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                shrapnelPath = dialog.FileName;
                workspaceView.LoadFromFile(shrapnelPath);
            }
        }

        private void OnSaveWorkspaceView(object sender, EventArgs e)
        {
            if (shrapnelPath == string.Empty)
            {
                OnSaveAsWorkspaceView(sender, e);
            }
            else
            {
                workspaceView.SaveToFile(shrapnelPath);
            }
        }

        private void OnSaveAsWorkspaceView(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();

            dialog.CheckFileExists = false;
            dialog.CheckPathExists = true;
            dialog.Filter = "Shrapnel Files (*.shrapnel)|*.shrapnel|All Files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            dialog.SupportMultiDottedExtensions = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                shrapnelPath = dialog.FileName;
                workspaceView.SaveToFile(shrapnelPath);
            }
        }

        private void OnPrintWorkspaceView(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void OnPrintPreviewWorkspaceView(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void OnPrintSetupWorkspaceView(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void OnExitWorkspaceView(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void deselectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Will be handled by the workspace view...
        }

        private void visibleToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
        {
            workspaceView.GridVisible = visibleToolStripMenuItem.Checked;
        }

        private void zoomToAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            workspaceView.ZoomAllInView();
        }

        private void zoomToselectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            workspaceView.ZoomSelectionInView();
        }

        private void toolbarToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
        {
            toolStrip.Visible = toolbarToolStripMenuItem.Checked;
        }

        private void statusBarToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void OnHelpContentsWorkspaceView(object sender, EventArgs e)
        {
        }

        private void OnHelpIndexWorkspaceView(object sender, EventArgs e)
        {
        }

        private void OnHelpSearchWorkspaceView(object sender, EventArgs e)
        {
        }

        private void OnHelpAboutWorkspaceView(object sender, EventArgs e)
        {
        }
    }
}